package com.shuhua.taskServer.controller;

import com.shuhua.pojo.entity.Announcement;
import com.shuhua.taskServer.service.AnnouncementService;
import com.shuhua.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    // 添加公告
    @PostMapping("/add")
    public Result<?> addAnnouncement(@RequestBody Announcement announcement) {
        announcementService.save(announcement);
        return Result.success();
    }

    // 返回所有公告
    @GetMapping("/all")
    public Result<List<Announcement>> getAllAnnouncements() {
        return Result.success(announcementService.list());
    }

    // 删除单个公告
    @DeleteMapping("/{id}")
    public Result<?> deleteAnnouncement(@PathVariable Long id) {
        announcementService.removeById(id);
        return Result.success();
    }

    // 编辑单个公告
    @PutMapping("/edit")
    public Result<?> editAnnouncement(@RequestBody Announcement announcement) {
        announcementService.updateById(announcement);
        return Result.success();
    }

    // 返回单条公告
    @GetMapping("/{id}")
    public Result<Announcement> getAnnouncementById(@PathVariable Long id) {
        return Result.success(announcementService.getById(id));
    }

    // 根据标题模糊搜索
    @GetMapping("/search")
    public Result<List<Announcement>> searchByTitle(@RequestParam String title) {
        return Result.success(announcementService.searchByTitle(title));
    }

    // 根据时间筛选（today/week/month）
    @GetMapping("/filter")
    public Result<List<Announcement>> filterByDate(@RequestParam String type) {
        // type: today/week/month
        return Result.success(announcementService.filterByDate(type));
    }
}